﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.UPMS.Core;
using ZDSoft.UPMS.Service.BusinessPart;
using ZDSoft.UPMS.Model.BusinessPart;
using Castle.ActiveRecord;
using NHibernate.Engine;
using System.Data.SqlClient;
using System.Data;
using NHibernate.Criterion;

namespace ZDSoft.UPMS.Web.Controllers
{
    public class ScoreController : Controller
    {
        //
        // GET: /Score/
        private string classes { get; set; }
        public ActionResult Index()
        {
            return View();
        }

        //  public ActionResult Index()
        // {
        //     IList<Organization> os= Container.Instance.Resolve<IOrganizationService>().FindAll();


        ////  string infos = Container.Instance.Resolve<IScoreService>().GetProc(98311, 196623);

        //     //ViewBag.score = infos;
        //  //return View();
        // }

        //public string GetInfos(int oId)
        //{
        //    return Container.Instance.Resolve<IScoreService>().GetProc(98311, oId);

        //}

        public string FindClass(string q)
        {

            IList<ICriterion> conditions = new List<ICriterion>();
            if (q != null)
            {
                q = q.ToUpper();
                conditions.Add(new LikeExpression("Name", "%" + q + "%"));
                IList<Organization> os = Container.Instance.Resolve<IOrganizationService>().Find(conditions).Where(a => a.Organizations.Count <= 0).ToList<Organization>();
                int aa = os.Count >= 9 ? 9 : os.Count;
                string info = "";
                for (int i = 0; i < aa; i++)
                {
                    info += os[i].Name + "," + os[i].ID + "\n";
                }
                if (info == "")
                {
                    return "未找到对应机构！,0";
                }
                else
                {
                    return info;
                }
            }
            else
            {
                return "noClass";
            }
        }

        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
          string strSearchCondition, string strSortProperty, string strSortCondition)
        {


            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }
            int pageNum;
            Organization o = Core.Container.Instance.Resolve<IOrganizationService>().FindAll().Where(i => i.Organizations.Count == 0).FirstOrDefault();
            IList<Model.Result> list = new List<Model.Result>();
            classes = o.Name;
            TempData["name"] = classes;
            int TermId = Core.Container.Instance.Resolve<ITermService>().GetCurrentTerm1().ID;
            IList<Model.Result> users = Core.Container.Instance.Resolve<IScoreService>().GetProc(TermId, o.ID);
            int count = users.Count;
            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Model.Result user in users)
            {
                list.Add(user);
            }
            list.Add(new Model.Result() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }

        public JsonResult GetPageds(int pageIndex, int pageSize, string strSearchProperty,
         string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            int oId = int.Parse(Request.QueryString["oId"].ToString());

            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }


            int pageNum;
            int TermId = Core.Container.Instance.Resolve<ITermService>().GetCurrentTerm1().ID;
            IList<Model.Result> list = new List<Model.Result>();
            IList<Model.Result> users = Core.Container.Instance.Resolve<IScoreService>().GetProc(TermId, oId);
            int count = users.Count;
            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Model.Result user in users)
            {
                list.Add(user);
            }
            list.Add(new Model.Result() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }
    }
}
